mod animation;
mod physics;
mod visual;
mod mover;
mod baton;
mod animover;
mod cannon;


use animation::Animation;
use ggez::{Context, GameResult, GameError, graphics, glam::Vec2};
use mover::Mover;

pub struct State{
    movers: Vec<Box<dyn animover::Animover>>,
    cannon: cannon::Cannon
}

impl State{
    pub fn new(_ctx: &mut Context) -> State {
        let movers: Vec<Box<dyn animover::Animover>> = vec![];
        let location = Vec2 { x: 30.0, y: 570.0 };
        let cannon = cannon::Cannon::new(location);

        State{ movers, cannon }
    }
}

impl ggez::event::EventHandler<GameError> for State {
    fn update(&mut self, ctx: &mut Context) -> GameResult {
        if ctx.mouse.button_just_pressed(ggez::event::MouseButton::Left) {
            let mut new_mover = baton::Baton::new(Vec2 { x: 30.0, y: 570.0 });
            new_mover.set_velocity(self.cannon.get_direction()*200.0/new_mover.get_mass());
            self.movers.push(Box::new(new_mover));
        }

        for mover in &mut self.movers {
            let fg = Vec2{x: 0.0, y: 1.0}*1.0*mover.get_mass();
            mover.apply_force(fg);
            let fz = -mover.get_velocity()*0.05;
            mover.apply_force(fz);

            mover.update(ctx);
        }
        self.cannon.update(ctx);
        Ok(())
    }
    fn draw(&mut self, ctx: &mut Context) -> GameResult {
        let mut canvas = graphics::Canvas::from_frame(ctx, graphics::Color::WHITE);

        for mover in &mut self.movers {
            mover.display(ctx, &mut canvas);
        }
        self.cannon.display(ctx, &mut canvas);
        canvas.finish(ctx)?;
        Ok(())
    }
}